const Base64 = require('js-base64').Base64
//获取打印列表
export const getPintList = (BrowserWindow) => {
  const list = BrowserWindow.getAllWindows()[0].webContents.getPrinters();
  return list.map((item) => {
    return {
      ...item,
      url: item.options["device-uri"]
    };
  });
};
//打印
export const pint = (BrowserWindow,data) => {
  return new Promise((resolve,reject) => {
    const win = new BrowserWindow({ width: 300, height: 300 })
    const viewUrl= `http://10.138.95.199:8012/onlinePreview?url=${encodeURIComponent(Base64.encode(data.url))}`
    win.loadURL(viewUrl).then(res=>{
      const options = {
        silent: true,
        deviceName:data.name,
        pageSize:data.pageSize,
        color: data.color,
        duplexMode: data.duplexMode,
        copies: data.copies,
        pageRanges: [ //打印范围
          {
            from: 0,
            to: 1,
          },
        ],
      };
      if(data.pageRanges == 0){
        delete options.pageRanges
      }else if(data.pageRanges.length == 1){
        options.pageRanges = [
          {
            from: Number(data.pageRanges),
            to: Number(data.pageRanges),
          },
        ]
      }else{
        let arr = data.pageRanges.split(',');
        options.pageRanges = [
          {
            from: Number(arr[0]),
            to: Number(arr[1]),
          },
        ]
      }
      
      setTimeout(()=>{
        win.webContents.print(options, (success, errorType) => {
          resolve(true)
          win.close()
        })
      },3000)
    })
  })
};

export const PINT_STATUS = ()=>{
  switch (x.printerStatusCode) {
    case 0: //正常
      x.printerStatus = "WORK_NORMAL";
      break;
    case 3: //正常
      x.printerStatus = "WORK_NORMAL";
      break;
    case 4: //缺纸
      x.printerStatus = "WORK_STATUS_NOPAPER";
      break;
    case 8: //脱机
      x.printerStatus = "PRINTER_STATUS_OFFLINE";
      break;
    default : //异常
      x.printerStatus = "PRINTER_STATUS_ABNORMALb";
      break;
  }
}